/**
 * Created by liujinghao on 2017/11/28.
 *
 */
 
var mapRoot = {}; // 地图的根对象
var tileLayer = new BMap.TileLayer();
tileLayer.getTilesUrl = function (tileCoord, zoom) {
    var x = tileCoord.x;
    var y = tileCoord.y;
    return 'image/tile-' + x + '_' + y + '.png';
}
var Qmap = new BMap.MapType('Qmap', tileLayer, {minZoom: 14, maxZoom: 16});
var homeMap = new BMap.Map('baidumap', {mapType: Qmap, enableMapClick: false});
//homeMap.addControl(new BMap.NavigationControl());
homeMap.centerAndZoom(new BMap.Point(113.573341369629, 35.6048202514648), 14);
homeMap.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
homeMap.disableInertialDragging(); // 禁用地图惯性拖拽
homeMap.disableDoubleClickZoom(); // 禁用双击放大
// homeMap.disableDragging(); // 禁止拖拽地图
homeMap.addEventListener("zoomend", function(e){
    // 获取当前级别
    var ZoomNum = homeMap.getZoom();
    console.log(ZoomNum)
    if (ZoomNum>14) {
        removeMarkerAll.call(mapRoot.landMarkObj);
        mapRoot.landAdd();
    }else{
        removeMarkerAll.call(mapRoot.landMarkObj);
        mapRoot.landAddData();
    }
    
});
//GPS坐标校准工具函数
function transA(coor) {
    var nc = coor - 0.000001;
    return nc
}
function transB(coor) {
    var nc = coor + 0.000001;
    return nc
}
function newBp(c1, c2) {
    return new BMap.Point(transA(c1), transB(c2))
}
function removeMarkerAll() {
    for(x in this){
        if (!this[x].remove) return;
       this[x].remove();
    }
}
// removeMarkerAll.call(markerPool.flashList)
// 测试坐标准确性
//  var point = newBp(113.575982,35.597342);
//  var marker = new BMap.Marker(point);  // 创建标注
//  homeMap.addOverlay(marker);               // 将标注添加到地图中
//  marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画

// 坐标测试数据
/* 点位坐标*/
var markCoor = {}
/*常态化显示的基础地标*/
markCoor.landMarkData = [
    {id: 'jichu0', name: '八里沟欢迎词', coor: '113.6294140000,35.5509870000',point_type: "1"},
    {id: 'jichu1', name: '绝壁天梯', coor: '113.54116179953,35.58937715608',point_type: "1"},
    {id: 'jichu2', name: '蘑菇石', coor: '113.52980721789,35.587704069894',point_type: "1"},
    {id: 'jichu3', name: '黑龙潭', coor: '113.52698653858,35.586500600124',point_type: "1"},
    {id: 'jichu4', name: '马武居', coor: '113.56922486366,35.604051838693',point_type: "1"},
    {id: 'jichu5', name: '红石河', coor: '113.5374890000,35.5910510000',point_type: "1"},
    {id: 'jichu6', name: '天河瀑布', coor: '113.5394650000,35.5912790000',point_type: "1"},
    {id: 'jichu7', name: '仙人迎客峰', coor: '113.5707620000,35.6047080000',point_type: "1"},
    {id: 'jichu8', name: '一线天', coor: '113.5456270000,35.5959530000',point_type: "1"},
    {id: 'jichu9', name: '玉皇宫', coor: '113.5553730000,35.6026820000',point_type: "1"},
    {id: 'jichu10', name: '丹凤台', coor: '113.59762928389,35.608380351512',point_type: "1"},
    {id: 'jichu11', name: '归真台', coor: '113.60916352664,35.60588598314',point_type: "1"},
    {id: 'jichu12', name: '清峰关', coor: '113.61083749165293,35.61584854428335',point_type: "1"},
    {id: 'jichu13', name: '老君庵', coor: '113.60498,35.610127',point_type: "1"},
    {id: 'jichu14', name: '戏龙台', coor: '113.59626385951,35.611329474063',point_type: "1"},
    {id: 'jichu15', name: '回龙挂壁', coor: '113.62208406878308,35.60563719338466',point_type: "1"},
    {id: 'jichu16', name: '老爷顶', coor: '113.60165369257,35.613808999679',point_type: "1"},
    {id: 'jichu17', name: '如画台', coor: '113.59371267187,35.615261466831',point_type: "1"},
    {id: 'jichu18', name: '闻琴台', coor: '113.59935403047,35.618092967699',point_type: "1"},
    {id: 'jichu19', name: '天梯', coor: '113.561564,35.650564',point_type: "1"},
    {id: 'jichu20', name: '西莲寺', coor: '113.560918,35.651855',point_type: "1"},
    {id: 'jichu21', name: '九莲潭', coor: '113.56013401191,35.649174039876',point_type: "1"}
]
// 创建地标maker
mapRoot.landMarkObj = {};// 所有地标
function landMarker(point, text) {
    // console.log(point)
    // console.log(text)
    this._point = point;
    this._text = text;
}
landMarker.prototype = new BMap.Overlay(); // 覆盖物的抽象基类
landMarker.prototype.initialize = function (map) {
    this._map = map;
    var div = this._div = document.createElement("div");
    div.classList.add('land');
    div.style.position = "absolute";
    div.innerHTML = this._text;
    homeMap.getPanes().labelPane.appendChild(div);
    return div;
}
landMarker.prototype.draw = function () {
    var map = this._map;
    // 根据地理坐标获取对应的覆盖物容器的坐标，此方法用于自定义覆盖物
    var pixel = map.pointToOverlayPixel(this._point);
    this._div.style.left = pixel.x + "px";
    this._div.style.top = pixel.y + "px";
}
mapRoot.landAddData = function (Ptype) {
    // removeMarkerAll.call(mapRoot.landMarkObj)
    console.log(markCoor.landMarkData);
    for (x in markCoor.landMarkData){
        var val= markCoor.landMarkData[x];
        // if (val.point_type == Ptype) {
            console.log(val.point_type)
        if (val.point_type) {
            mapRoot.landMarkObj[val.id] = new landMarker(new BMap.Point(val.coor.split(',')[0], val.coor.split(',')[1]), val.name);
            // addOverlay 将覆盖物添加到地图中，一个覆盖物实例只能向地图中添加一次
            homeMap.addOverlay(mapRoot.landMarkObj[val.id]);
        }
    }
}
mapRoot.landAddData();
// 批量添加基础 地标
mapRoot.landAdd = function (Ptype) {
    // removeMarkerAll.call(mapRoot.landMarkObj)
    console.log(markCoor.landMark);
    for (x in markCoor.landMark){
        var val= markCoor.landMark[x];
        // if (val.point_type == Ptype) {
            console.log(val.point_type)
        if (val.point_type) {
            mapRoot.landMarkObj[val.id] = new landMarker(new BMap.Point(val.coor.split(',')[0], val.coor.split(',')[1]), val.name);
            // addOverlay 将覆盖物添加到地图中，一个覆盖物实例只能向地图中添加一次
            homeMap.addOverlay(mapRoot.landMarkObj[val.id]);
        }
    }
}
// mapRoot.landAdd();
// 景点与演出类型的切换
$("#mapTypeBtn .btn").on("click",function () {
     var go=$(this).attr('go');
    $("#mapTypeBtn").attr('class',go);
    if(go=='maptype1'){
        mapRoot.landAdd(2);
    }else {
        mapRoot.landAdd(1);
    }
})


// 获取基础坐标数据
// loadData({
//     type: 'get',
//     url: '/bigdata/manage/map/get-point',
//     interval: 0,
//     callback: function (res) {
//         console.log('基础点位数据', res)
        
//         markCoor.landMark = [];
//         res.result.forEach(function (val, index) {
//             var obj = {};
//             obj.id = 'jichu' + index;
//             obj.name = val.name;
//             obj.coor = val.coord;
//             obj.point_type=val.point_type;
//             markCoor.landMark.push(obj)
//         })
//         res=null;
//         // mapRoot.landAdd(2);// 添加基础地标
//     }
// })

 function getBaseData (res) {
    markCoor.landMark = [];
        res.result.forEach(function (val, index) {
            var obj = {};
            obj.id = 'jichu' + index;
            obj.name = val.name;
            obj.coor = val.coord;
            obj.point_type=val.point_type;
            markCoor.landMark.push(obj)
        })
        res=null;
}



// 彩色会闪动的marker
mapRoot.coMarkObj = {};// 所有彩色点的映射
function coMarker(point, bg, name) {
    this._point = point;
    this._bg = bg;
    this._name = name;
}
coMarker.prototype = new BMap.Overlay();
coMarker.prototype.initialize = function (map) {
    this._map = map;
    var div = this._div = document.createElement("div");
    div.classList.add('comark');
    div.setAttribute('names', this._name);
    div.style.position = "absolute";
    div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);

    var span = document.createElement('span');
    span.style.backgroundColor = this._bg;
    div.appendChild(span);
    var em = document.createElement('em');
    em.style.backgroundColor = this._bg;
    div.appendChild(em);
    var strong = document.createElement('strong');
    strong.style.borderColor = this._bg;
    div.appendChild(strong);
    // 返回地图覆盖物容器列表
    homeMap.getPanes().labelPane.appendChild(div);
    return div;

}
// 抽象方法，当地图状态发生变化时，由系统调用对覆盖物进行绘制。自定义覆盖物需要实现此方法
coMarker.prototype.draw = function () {
    var map = this._map;
    var pixel = map.pointToOverlayPixel(this._point);
    if(!this._div) return
    this._div.style.left = pixel.x + "px";
    this._div.style.top = pixel.y + "px";
}
coMarker();

//  简单图标类型的marker
function iconMarker(point, imgURL,w,h) {
    this._point = point;
    this._imgURL = imgURL;
    this._w=15;
    this._h=h||15;
}
iconMarker.prototype = new BMap.Overlay();
iconMarker.prototype.initialize = function (map) {
    this._map = map;
    var div = this._div = document.createElement("div");
    div.classList.add('iconMarkerDiv');
    div.style.position = "absolute";
    div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
    var marginPX= -(this._w)/2;
    div.style.marginLeft = marginPX+'px'
    div.style.marginTop = marginPX + 'px';
    
    var img = document.createElement('img');
    img.src=this._imgURL;
    img.width= this._w;
    img.height= this._h;
    div.appendChild(img);

    homeMap.getPanes().labelPane.appendChild(div);
    return div;
}
iconMarker.prototype.draw = function () {
    var map = this._map;
    var pixel = map.pointToOverlayPixel(this._point);
    this._div.style.left = pixel.x + "px";
    this._div.style.top = pixel.y + "px";
}



//  轮询闪烁点位
// loadData({
//     type: 'get',
//     url: '/bigdata/manage/voice/get-flash-point',
//     interval: 3000,
//     callback: function (res) {
//         var data=JSON.parse(JSON.stringify(res.result))
//         if (res.result.list.length == 0) {
            
//         }else{
//             flash_marker_render(data)
//         }
//         res=null;
//     }
// })

 var markerPool = {};  //动态点位对象池
// function flash_marker_render(para){
//  removeMarkerAll.call(markerPool.boatList);// 先清除上次显示的所有闪烁点位
//  removeMarkerAll.call(markerPool.patrolList);// 先清除上次显示的所有闪烁点位
//  removeMarkerAll.call(markerPool.flashList);// 先清除上次显示的所有闪烁点位
//     markerPool.boatList = []; // 游船
//  markerPool.patrolList = []; // 巡更人员
//  markerPool.flashList = [];// 闪烁点

    
//     /*para.night_patrol_list.forEach(function (val, index) {
//         var pt = new BMap.Point(val.coord.split(',')[0], val.coord.split(',')[1]);
//         var imgURL = 'http://static-1251225286.cossh.myqcloud.com/qingdata/xungeng.png'
//         markerPool.patrolList[index] = new iconMarker(pt, imgURL);
//         homeMap.addOverlay(markerPool.patrolList[index]);
//     })*/


//     //  其他类型点位
//     var colorList={
//         gcspa: '#0c7fd8',
//         wywzx: '#da1b24',
//         yyhc: '#fbed47',
//         xgry: '#7a53fc',
//     }
//    para.list.forEach(function (val,index) {
//         var color=colorList[val.type]
//         markerPool.flashList[index]= new coMarker(new BMap.Point(val.coord.split(',')[0], val.coord.split(',')[1]), color, val.name+index);
//         homeMap.addOverlay(markerPool.flashList[index]);
//     })
// }

 let resData={
    code: 0, 
    msg: "", 
    result:[{point_type: "2", type: "jddw", name: "后静宫", coord: "113.57608596611,35.648342536509", is_show: "1"}
    ,{point_type: "2", type: "jddw", name: "九莲潭", coord: "113.56013401191,35.649174039876", is_show: "1"}
    ,{point_type: "2", type: "jddw", name: "天壶瀑布", coord: "113.560397,35.650359", is_show: "1"}
    ,{point_type: "2", type: "jddw", name: "玄武庙", coord: "113.60127640426,35.616009697099", is_show: "1"}
    ,{point_type: "2", type: "jddw", name: "求官台", coord: "113.60740284784,35.612517895425", is_show: "1"}
    ,{point_type: "2", type: "jddw", name: "将军峰", coord: "113.60731301729,35.61134414604", is_show: "1"}
    ,{point_type: "2", type: "jddw", name: "蜡烛峰", coord: "113.60894793331,35.608879215771", is_show: "1"}
    ,{point_type: "2", type: "jddw", name: "乌鸦岭", coord: "113.60621708456,35.605533830697", is_show: "1"}
    ,{point_type: "2", type: "jddw", name: "归真台", coord: "113.60916352664,35.60588598314", is_show: "1"}
    ,{point_type: "2", type: "jddw", name: "丹凤台", coord: "113.59762928389,35.608380351512", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "闻琴台", coord: "113.59935403047,35.618092967699", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "听禅台", coord: "113.606288949,35.616244434552", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "试胆台", coord: "113.60366589692,35.616347131969", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "如画台", coord: "113.59371267187,35.615261466831", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "戏龙台", coord: "113.59626385951,35.611329474063", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "老爷顶", coord: "113.60171070767,35.613779786187", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "清峰关", coord: "113.61085545776,35.615789859692", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "老君庵", coord: "113.60498,35.610127", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "救帝桑", coord: "113.5384600000,35.5915940000", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "猕猴保护区", coord: "113.5552120000,35.5962900000", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "一线天", coord: "113.5456270000,35.5959530000", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "桃花禅院", coord: "113.57096757635,35.604007818705", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "马武居", coord: "113.56922486366,35.604051838693", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "百年老屋", coord: "113.54080247733,35.59310478265", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "珍珠瀑", coord: "113.52729196246,35.587366513142", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "黑龙潭", coord: "113.52698653858,35.586500600124", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "蘑菇石", coord: "113.52980721789,35.587704069894", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "百年好合树", coord: "113.53873637465,35.590492527272", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "点将台", coord: "113.5439285805,35.589054282545", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "绝壁天梯", coord: "113.54116179953,35.58937715608", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "羊洲地", coord: "113.5492106169,35.593809196343", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "西莲寺", coord: "113.560918,35.651855", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "天梯", coord: "113.561564,35.650564", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "情侣石", coord: "113.5387280000,35.5875590000", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "回龙挂壁", coord: "113.62208406878,35.605637193385", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "老子布道", coord: "113.5685063212,35.605279837", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "静心潭", coord: "113.5666570000,35.6030960000", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "玉皇宫", coord: "113.5553730000,35.6026820000", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "仙人迎客峰", coord: "113.56576742136,35.606145861398", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "天河瀑布", coord: "113.5394650000,35.5912790000", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "桃花湾瀑布桃花潭", coord: "113.57434113556,35.604443227215", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "桃花湾", coord: "113.5755753055,35.605180687325", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "红石河", coord: "113.5374890000,35.5910510000", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "抱犊桥将军潭", coord: "113.57067740668,35.604434781169", is_show: "1"}
    , {point_type: "2", type: "jddw", name: "八里沟欢迎词", coord: "113.6294140000,35.5509870000", is_show: "1"}]
 }

 getBaseData(resData)
